package com.gree.controller;

import java.util.Date;
import java.io.PrintWriter;
import com.gree.utils.MessageID;
import com.gree.utils.SecretTool;
import com.gree.utils.StringTool;
import java.text.SimpleDateFormat;
import com.gree.utils.UniqueIDTool;
import com.gree.entity.CmnC006User;
import com.gree.utils.DateTimeTool;
import org.springframework.ui.Model;
import javax.servlet.http.HttpSession;
import com.gree.service.ICmnC006UserService;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
	@Autowired
	private ICmnC006UserService cmnC006UserService;

	@RequestMapping(value = "/login/land", method = RequestMethod.GET)
	public String login() {
		return "login";
	}

	/**
	 * 登陆
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/login/auth", method = RequestMethod.POST)
	public void login(String account, String passwords, HttpSession session,HttpServletResponse response) throws Exception {
		CmnC006User obj = new CmnC006User();
		obj.setLoginId(account);
		CmnC006User user = cmnC006UserService.getUserByObj(obj);
		// r是返回的用户id，根据你自己生成的id，修改下方if判断条件
		String result = null;
		if (user == null) {
			result = StringTool.getJSONObject("error", MessageID.NOT_EXIST_USER.replace("#LOGIN_ID#", account));
		} else {
			if (user.getLoginErrorCount() >= 5) {
				result = StringTool.getJSONObject("counts", MessageID.USER_LOCK.replace("#LOGIN_ID#", account));
			} else {
				if (!user.getLoginPassword().equals(SecretTool.md5Encode(user.getUserId() + passwords))) {
					CmnC006User enti = new CmnC006User();
					enti.setUserId(user.getUserId());
					enti.setLoginErrorCount(user.getLoginErrorCount() + 1);
					enti.setUpdateFlag(UniqueIDTool.getUniqueID());
					enti.setLastUpdateUserId(user.getUserId());
					enti.setLastUpdateTime(DateTimeTool.getStandardTime());
					cmnC006UserService.updateNonEmptyCmnC006UserById(enti);
					result = StringTool.getJSONObject("errors",MessageID.ERR_PASSWORD);
				} else {
					CmnC006User enti = new CmnC006User();
					enti.setUserId(user.getUserId());
					enti.setLastLoginTime(DateTimeTool.getStandardTime());
					enti.setUpdateFlag(UniqueIDTool.getUniqueID());
					enti.setLastUpdateUserId(user.getUserId());
					enti.setLastUpdateTime(DateTimeTool.getStandardTime());
					cmnC006UserService.updateNonEmptyCmnC006UserById(enti);
					// 用户登陆成功
					session.setAttribute("user", user);// 对象
					session.setAttribute("userid", user.getUserId());
					//session.setAttribute("times",stampToDate(user.getLastLoginTime()==null?user.getCreateTime():user.getLastLoginTime()));
					result = StringTool.getJSONObject("success", "");
				}
			}
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(result);// 返回登录信息
		out.flush();
		out.close();
	}

	/** 退出 */
	@RequestMapping(value = "/login/out", method = RequestMethod.GET)
	public String logOut(Model model, HttpSession session) {
		// 通过session.invalidata()方法来注销当前的session
		session.invalidate();
		model.asMap().remove("user");
		return "redirect:/index.jsp";// 页面
	}

	public static String stampToDate(Long lt) {
		String pattern = "yyyy年MM月dd日  HH时mm分ss秒";
		SimpleDateFormat simp = new SimpleDateFormat(pattern);
		Date date = new Date(lt);
		return simp.format(date);
	}
}
